package com.bw.a2207a.view

import android.os.CountDownTimer
import android.widget.ImageView
import com.alibaba.android.arouter.launcher.ARouter
import com.bumptech.glide.Glide
import com.bw.a2207a.R
import com.bw.a2207a.base.BaseActivity
import com.bw.a2207a.databinding.ActivityWelcomeBinding
import com.bw.a2207a.viewmodel.LoginViewModel
import com.tencent.mm.opensdk.openapi.IWXAPI


class WelcomeActivity :
    BaseActivity<ActivityWelcomeBinding, LoginViewModel>(R.layout.activity_welcome) {

    //倒计时
    lateinit var countDownTimer: CountDownTimer
    override fun initData() {
        viewModel.bannerLiveData.observe(this) {
            if (it.code == 200) {
                binding.banner.setData(it.data, null)
                binding.banner.setAutoPlayAble(true)
                binding.banner.setAutoPlayTime(1200)
                binding.banner.setmAdapter { banner, _, view, position ->
                    Glide.with(this@WelcomeActivity).load(it.data.get(position).imagePath)
                        .into(view as ImageView)
                }
            }
        }
    }

    private var api: IWXAPI? = null

    override fun initView() {

        countDownTimer = object : CountDownTimer(3000, 1000) {
            override fun onTick(time: Long) {
                binding.tv.text = "${time / 1000} 后跳转"
            }

            override fun onFinish() {
                countDownTimer.cancel()

                ARouter.getInstance()
                    .build("/main/MainActivity")//目标页面的path
                    .withInt("key", 1)
                    .withString("userName", "zs")
                    .withString("userName1", "zs")
                    .navigation()


                countDownTimer.cancel()
//            startActivity(Intent(this@WelcomeActivity, LoginActivity::class.java))
                finish()
//                startActivity(Intent(this@WelcomeActivity, LoginActivity::class.java))
//                finish()
            }
        }
        //开启倒计时
        countDownTimer.start()

        //获取轮播图
        //  viewModel.getBanner()
        binding.tv.setOnClickListener {
            ARouter.getInstance()
                .build("/main/MainActivity")//目标页面的path
                .withInt("key", 1)
                .withString("userName", "zs")
                .withString("userName1", "zs")
                .navigation()
//            startActivity(Intent(this@WelcomeActivity, LoginActivity::class.java))
            finish()
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        countDownTimer.cancel()
    }

}